//
//  WQViewController.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/9.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit
import SDWebImage
class WQWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView.image = UIImage(named: "ad_background")
        // 异步加载用户头像
        iconView.sd_setImageWithURL(WQAccountViewModel.sharedAccount.profileIconUrl,
            placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        setupUI()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//        iconView.sd_setImageWithURL(WQAccountViewModel.sharedAccount.profileIconUrl)
        iconView.alpha = 0.8
        welcomeLabel.alpha = 0
        
        ///  头像和欢迎label动画
        iconView.snp_updateConstraints(closure: { (make) -> Void in
             make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
            })
        UIView.animateWithDuration(0.5, animations: { () -> Void in
             self.view.layoutIfNeeded()
            }) { [weak self] (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self!.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                   delay(0.5, callFunc: { () -> () in
                    ///  完成通知appDelegate切换控制器
                    WQNotificationCenter.postNotificationName(WQSwitchRootViewControllerNotification, object: nil)
                   })
                })
                
                
                
                
        }
    }

    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        

    }
    
    //替换默认的view
    override func loadView() {
        view = imageView
    }

    //欢迎界面背景图片
    lazy var imageView = UIImageView()
    //头像
    lazy var iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //label
    lazy var welcomeLabel = UILabel(title: "欢迎归来", color: UIColor.darkGrayColor())
}
